﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace HuiZe.education.View.Ethan
{
    /// <summary>
    /// PART_Media_Lyric.xaml 的交互逻辑
    /// </summary>
    public partial class PART_Media_Lyric : UserControl
    {
        public int[] LyricMillSeconds
        {
            get { return _dic_target.Keys.ToArray(); }
        }

        public PART_Media_Lyric()
        {
            InitializeComponent();
            _MoveLyricStoryboard = (Storyboard)this.FindResource("MoveLyricStoryboard");
            _MoveLyricStoryboard.Completed += _MoveLyricStoryboard_Completed;
        }

        public void SetLyric(string _lyric_txt)
        {
            _index_current = 0;
            _is_animation_completed = true;
            _dic_target = new Dictionary<int, int>();
            _list_lyric = new List<model_lyric_info>();
            _lyrics = new ObservableCollection<model_lyric_info>();
            if (!string.IsNullOrEmpty(_lyric_txt))
            {
                string[] _list = _lyric_txt.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string _line in _list)
                {
                    if (string.IsNullOrEmpty(_line))
                        continue;

                    string _txt = _line.Replace("\r", "");
                    string _lyric = _txt.Split(']')[1];
                    string _time = _txt.Split(']')[0].Substring(1);
                    int _minute = Convert.ToInt32(_time.Split(':')[0]);
                    int _second = Convert.ToInt32(_time.Split(':')[1].Split('.')[0]);
                    int _millisecond = Convert.ToInt32(_time.Split('.')[1]);

                    _list_lyric.Add(new model_lyric_info
                    {
                        lyric = _lyric,
                        minute = _minute,
                        second = _second,
                        millisecond = _millisecond,
                    });
                }
                if (_list_lyric.Count > 0)
                    _list_lyric = _list_lyric.OrderBy(x => x.total_millisecond).ToList();
            }

            if (_list_lyric.Count == 0)
            {
                GridPrev.DataContext = null;
                GridNow.DataContext = null;
                GridNext.DataContext = null;
            }
            else
            {
                GridPrev.DataContext = null;
                GridNow.DataContext = _list_lyric[0];
                GridNext.DataContext = _list_lyric.Count > 1 ? _list_lyric[1] : null;
                for (int i = 0; i < _list_lyric.Count; i++)
                {
                    if (!_dic_target.ContainsKey(_list_lyric[i].total_second))
                        _dic_target.Add(_list_lyric[i].total_second, i);
                }
                _lyrics = new ObservableCollection<model_lyric_info>(_list_lyric.Skip(2));
                PART_Lyric_List.ItemsSource = _lyrics;
            }
        }

        public void MoveNext(int _millsecond)
        {
            if (!_dic_target.ContainsKey(_millsecond))
                return;

            if (!_is_animation_completed) return;

            _is_animation_completed = false;
            _index_current = _dic_target[_millsecond];
            int _animation_milliseconds = GetLyricAnimationMillSeconds(_index_current) / 2;
            if (_animation_milliseconds <= 0) _animation_milliseconds = 100;
            if (_animation_milliseconds > 1500) _animation_milliseconds = 1500;

            foreach (var _child in _MoveLyricStoryboard.Children)
            {
                _child.Duration = new Duration(TimeSpan.FromMilliseconds(_animation_milliseconds));
            }
            _MoveLyricStoryboard.Begin();
        }

        void _MoveLyricStoryboard_Completed(object sender, EventArgs e)
        {
            GridNow.DataContext = _list_lyric[_index_current];
            if (_list_lyric.Count == 1)
            {
                GridPrev.DataContext = null;
                GridNext.DataContext = null;
            }
            else
            {
                GridPrev.DataContext = _index_current == 0 ? null : _list_lyric[_index_current - 1];
                GridNext.DataContext = _index_current == (_list_lyric.Count - 1) ? null : _list_lyric[_index_current + 1];
            }
            if (_lyrics.Count > 0)
                _lyrics.RemoveAt(0);

            _is_animation_completed = true;
        }

        int GetLyricAnimationMillSeconds(int _index)
        {
            if (_index >= 0 && _index < (_list_lyric.Count - 1))
            {
                return _list_lyric[_index + 1].total_millisecond - _list_lyric[_index].total_millisecond;
            }
            return 0;
        }

        int _index_current;
        bool _is_animation_completed;
        Dictionary<int, int> _dic_target;
        Storyboard _MoveLyricStoryboard;
        IList<model_lyric_info> _list_lyric;
        ObservableCollection<model_lyric_info> _lyrics;
    }
}
